SQL 及 R 語言的資料分析

郭耀仁

你在咖啡廳做什麼?

準備明天開會的材料

連個資料庫

撈個資料

SELECT Continent
      ,sum(Population)
      FROM world.country
      GROUP BY Continent;

將資料庫查詢的結果輸出成為 csv

把 csv 匯入到 Excel 中作整理與分析

表格與圖貼到 Powerpoint 上

簡報大展身手

主管、客戶覺得你的提案好棒棒

將分析用的程式與圖表整理成文件存參

如果是 Chandler Bing 會怎麼做?

設定 R 與資料庫的介接

library(DBI)

# 利用 `dbConnect()` 建立連線
con <- dbConnect(RMySQL::MySQL(), 
                 dbname = "world",
                 host = "rsqltrain.ced04jhfjfgi.ap-northeast-1.rds.amazonaws.com",
                 port = 3306,
                 user = "trainstudent",
                 password = "csietrain")

將資料匯入 R 語言

country <- dbReadTable(con, "country")
dbDisconnect(con)

作整理與分析

library(dplyr)
library(ggplot2)

query_df <- country %>%
  group_by(Continent) %>%
  summarise(ttl_pop = sum(as.numeric(Population))) %>%
  arrange(ttl_pop)

query_df$Continent <- factor(query_df$Continent, levels = query_df$Continent)
bar_plot <- ggplot(query_df, aes(x = Continent, y = ttl_pop, fill = Continent)) +
  geom_bar(stat = "identity", alpha = 0.5) +
  xlab("") +
  ylab("") +
  theme_minimal() +
  coord_flip() +
  ggtitle("Population across continents")

直接輸出簡報

簡報也是好棒棒

改輸出格式成為文件

Why R?

免費又好裝

不強調物件導向

內外兼修

溝通、溝通、還是溝通

活躍的社群支援

課程目標

  • 拉近使用者與資訊人員的距離
  • 提高使用者的工作掌握度

課程結束後你將能夠

  • 自行撰寫 SQL 指令查詢
  • 進行更有效率的需求訪談
  • 應用 R 語言於資料探索、作圖與報告

一起輕鬆學習 R 與資料科學